<?php
if(!defined('DEDEINC')) exit('Request Error!');


/**
 * 网页元素标签
 * 
 * 支持的参数：

 * row		显示数量
 * channel  查看channeltype表  -1为专题页  杂志为7
 * typeid	主分类（栏目） 可选
 * id       编号              可选
 * infolen	描述字数，可选
 * titlelen	标题字数，可选
 * flag     标志 如:推荐  可选

 * 
 * 使用方式：
 * {dede:splist row=1 typeid=0 titlelen=30 infolen=50 channel=-1}
 * ....[field:字段名/]....
 * {/dede:splist}
 * 字段名列表：
 * id: 编号
 * title: 标题
 * shorttitle: 简略标题
 * flag: 标志    [条件]
 * litpic: 缩略图
 * writer: 责任编辑
 * color: 标题颜色
 * typeid：主分类     [条件 栏目]
 * keywords:关键字
 * description 专题简介
 * pubdate   发布时间
 * click  浏览次数
 * key    序号
 * 
 */

function lib_splist(&$ctag,&$refObj){
	 global $dsql;
	 
	 //属性处理
    $attlist="row|1,channel|-1,typeid|0,id|0,infolen|0,titlelen|0,flag|0";
    FillAttsDefault($ctag->CAttribute->Items,$attlist);
    extract($ctag->CAttribute->Items, EXTR_SKIP);
    
    $innertext = $ctag->GetInnerText();
    $row = !empty($row) ? $row : 1;
    $where = " 1";
    if ($typeid){
   		 $where .= " and typeid='$typeid' ";
    }
    if ($channel){
    	 $where .= " and channel='$channel' ";
    }
    if ($id){
    	$where .= " and id=".$id;
    }
    if ($flag){
    	$where .= " and flag='$flag'";
    }
    
    $ctp = new DedeTagParse();
    $ctp->SetNameSpace('field', '[', ']');
    
    $html = '';
    $query = "SELECT id AS id, title, shorttitle,litpic as img_url,writer,color,
    keywords,description as info,pubdate,click FROM `#@__archives` WHERE ".$where." ORDER BY `id` ASC LIMIT 0,".$row;
    $dsql->Execute('me',$query);
    $ctp->LoadSource($innertext);
    $key = 0;
    while($rs = $dsql->GetArray('me')){
    	if($titlelen) $rs['title'] = cn_substr($rs['title'], $titlelen);
    	if($infolen) $rs['info'] = cn_substr($rs['info'], $infolen);
    	$rs['pubdate'] = date('Y-m-d H:i:s',$rs['pubdate']);
    	$rs['key'] = ++$key;
    	foreach($ctp->CTags as $tagid=>$ctag)
        {
            if(isset($rs[$ctag->GetName()]))
            {
                $ctp->Assign($tagid,$rs[$ctag->GetName()]);
            }
        }
        $html .= $ctp->GetResult();
    }
    return $html;
}
?>